/*
 * Copyright 2022 Pnoker All Rights Reserved
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rundow.center.manager.service.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.rundow.center.manager.mapper.DeviceEventMapper;
import com.rundow.center.manager.mapper.DriverEventMapper;
import com.rundow.center.manager.service.EventService;
import com.rundow.common.base.bean.Pages;
import com.rundow.common.base.dto.DeviceEventDto;
import com.rundow.common.base.dto.DriverEventDto;
import com.rundow.common.base.model.event.DeviceEvent;
import com.rundow.common.base.model.event.DriverEvent;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author pnoker
 */
@Slf4j
@Service
public class EventServiceImpl implements EventService {

    @Resource
    private DeviceEventMapper deviceEventMapper;

    @Resource
    private DriverEventMapper driverEventMapper;
    @Override
    public void addDriverEvent(DriverEvent driverEvent) {
        if (ObjectUtil.isNotNull(driverEvent)) {
            driverEventMapper.insert(driverEvent);
        }
    }

    @Override
    public void addDriverEvents(List<DriverEvent> driverEvents) {
        if (null != driverEvents) {
            if (driverEvents.size() > 0) {
                driverEvents.forEach(driverEvent -> addDriverEvent(driverEvent));

            }
        }
    }

    @Override
    public void addDeviceEvent(DeviceEvent deviceEvent) {
        if (ObjectUtil.isNotNull(deviceEvent)) {
            deviceEventMapper.insert(deviceEvent);
        }
    }

    @Override
    public void addDeviceEvents(List<DeviceEvent> deviceEvents) {
        if (null != deviceEvents) {
            if (deviceEvents.size() > 0) {
                deviceEvents.forEach(deviceEvent -> addDeviceEvent(deviceEvent));
            }
        }
    }

    @Override
    public Page<DriverEvent> driverEvent(DriverEventDto driverEventDto) {

        return null;
    }

    @Override
    public Page<DeviceEvent> deviceEvent(DeviceEventDto deviceEventDto) {
        final Pages page = deviceEventDto.getPage();
        if (ObjectUtil.isNull(page)) {
            deviceEventDto.setPage(new Pages());
        }
        final long startTime = page.getStartTime();
        final long endTime = page.getEndTime();
        LambdaQueryWrapper<DeviceEvent> queryWrapper = Wrappers.<DeviceEvent>query().lambda();
        queryWrapper.eq(StrUtil.isNotEmpty(deviceEventDto.getDeviceId()), DeviceEvent::getDeviceId, deviceEventDto.getDeviceId());
        queryWrapper.eq(StrUtil.isNotEmpty(deviceEventDto.getPointId()), DeviceEvent::getPointId, deviceEventDto.getPointId());
        queryWrapper.ge(startTime>0,DeviceEvent::getOriginTime,deviceEventDto.getConfirmTime());
        queryWrapper.le(endTime>0,DeviceEvent::getOriginTime,deviceEventDto.getConfirmTime());
        Page<DeviceEvent> deviceEventDtoPage = deviceEventMapper.selectPage(page.convert(), queryWrapper);
        return  deviceEventDtoPage;

    }




}
